// plugin/subpages1/orderDetail/orderDetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    app.setNavtitle('竞拍详情');
    if(e&&e.tid){
      that.setData({
        tid:e.tid
      })
    }
  },
  onShow:function(){
    var that = this;
    that.requestDetail();
  },
  requestDetail: function () {
    var that = this;
    var data = {
      map: 'applet_auction_order_detail',
      tid: that.data.tid
    };
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            orderInfo: res.data.data,
            payMoney: parseFloat(res.data.data.balance),
            hasData: 1
          })
          var leftTime = res.data.data.leftTime;
          that.countDjs(leftTime);
        } else {
          console.log(res.data)
          that.setData({
            auctionInfo: res.data.data,
            hasData: 0
          })
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  makeCall:function(e){
    var mobile = e.currentTarget.dataset.mobile;
    console.log(mobile);
    app.makeCallphone(mobile);
  },
  countDjs: function (leftTime) {
    var that = this;
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10); //计算剩余的天数 
      var hours = parseInt(leftTime / 60 / 60 % 24, 10); //计算剩余的小时 
      var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟 
      var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数 
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      leftTime--;
      if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(that.data.intervarID);
        leftTime = 0;
      }
    } else {
      that.setData({
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00',
      })
      clearInterval(that.data.intervarID);
      leftTime = 0;
    }
    that.data.intervarID = setTimeout(function () {
      that.countDjs(leftTime);
    }, 1000)
  },
  tologisticsInfo: function (e) {
    var tid = e.currentTarget.dataset.tid;
    wx.navigateTo({
      url: '/subpages0/auctionpage/logisticsInfo/logisticsInfo?tid=' + tid
    })
  },
  tosubmitOrder: function (e) {
    var tid = e.currentTarget.dataset.tid;
    wx.navigateTo({
      url: '/subpages0/auctionpage/submitOrder/submitOrder?type=retainage&tid=' + tid
    })
  },
  toauctionDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages0/auctionpage/auctionDetail/auctionDetail?id=' + id,
    })
  },
  delOrder: function (e) {
    var that = this;
    var orderId = that.data.orderInfo.tid;
    wx.showModal({
      title: '删除提示',
      cancelText: '取消',
      confirmText: '确认',
      content: '确认删除该拍单吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_delete',
              tid: orderId
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                wx.navigateBack({
                  delta:1
                })
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  confirmReceiving: function () { //触发订阅消息
    var that = this;
    app.getSubId(that, 'applet_order_confirm_accept', 'toConfirmReceiving');
  },
  toConfirmReceiving: function (e) {
    var that = this;
    var orderId = that.data.orderInfo.tid;
    wx.showModal({
      title: '',
      cancelText: '还没收到',
      confirmText: '确认收货',
      content: '确认收到货物了吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_confirm_accept',
              tid: orderId
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                that.requestDetail();
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})